package com.renjibo.sharkerwave.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.kevin.wraprecyclerview.WrapAdapter;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.adapter.GreatesAdapter;
import com.renjibo.sharkerwave.adapter.HomeAdapter;
import com.renjibo.sharkerwave.models.Course;
import com.renjibo.sharkerwave.models.data.CourseData;
import com.renjibo.sharkerwave.network.SharkerParams;
import com.renjibo.sharkerwave.ui.view.DividerItemDecoration;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 专辑列表展示页面
 */
public class GreatestHitsActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView greatest_recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_greatest_hits);
        Toolbar greatest_toolbar = (Toolbar) findViewById(R.id.greatest_toolbar);
        setSupportActionBar(greatest_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        initView();
        listTopic("list_topic", 20, 0);
    }

    public void initView() {
        greatest_recyclerView = (RecyclerView) findViewById(R.id.greatest_recyclerView);
        ImageView greatest_imageView_left = (ImageView) findViewById(R.id.greatest_imageView_left);
        greatest_imageView_left.setOnClickListener(this);
        greatest_recyclerView.setLayoutManager(new LinearLayoutManager(this));

        greatest_recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        Log.d("zzz", "chengong");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击关闭此页面
            case R.id.greatest_imageView_left:
                finish();
                break;
        }
    }

    /**
     * 精品专辑列表
     *
     * @param uri
     * @param page_index
     */
    void listTopic(String uri, int page_size, int page_index) {
        listCourse(uri, "", page_size, page_index, 0);
    }

    /**
     * 网络请求
     *
     * @param uri
     * @param category
     * @param page_index
     * @param sort_by
     */
    public void listCourse(final String uri, final String category, int page_size, int page_index, int sort_by) {
        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", page_size + "");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<CourseData>() {
            @Override
            public void onSuccess(CourseData result) {
                greatest_recyclerView.setAdapter(new GreatesAdapter(GreatestHitsActivity.this,result.topicCourse));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
